package neusoft.edu.cn.a215lmeordering.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.ViewHolder.CollectionItemViewHolder;
import neusoft.edu.cn.a215lmeordering.activities.FoodDetailsActivity;
import neusoft.edu.cn.a215lmeordering.activities.ShopFoodActivity;
import neusoft.edu.cn.a215lmeordering.bean.CollectionList_Food;
import neusoft.edu.cn.a215lmeordering.model.CollectionFoodModel;

public class CollectionFoodAdapter extends RecyclerView.Adapter<CollectionItemViewHolder> {
    private List<CollectionList_Food> shopDateList;
    private int food_id,user_id;

    public void setDate(List<CollectionList_Food> dateItemList){
        this.shopDateList=dateItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public CollectionItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = layoutInflater.inflate(R.layout.item_collect_layout,viewGroup,false);
        CollectionItemViewHolder viewHolder = new CollectionItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull final CollectionItemViewHolder itemViewHolder, final int i) {
        final CollectionList_Food collection_List_food = shopDateList.get(i);

        food_id=collection_List_food.getFood_id();
        user_id=collection_List_food.getUser_id();

        itemViewHolder.collect_foodname.setText(collection_List_food.getFoodname());
        itemViewHolder.collect_foodprice.setText(""+ collection_List_food.getPrice());

        Picasso.get()
                .load("http://172.24.10.175:8080/foodService"+ collection_List_food.getPic())
                .into(itemViewHolder.imageView);

        itemViewHolder.jinru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(itemViewHolder.jinru.getContext(), FoodDetailsActivity.class);
                intent.putExtra("food_id",collection_List_food.getFood_id());
                itemViewHolder.jinru.getContext().startActivity(intent);
            }
        });

        itemViewHolder.centel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectionFoodModel collectionFoodModel = new CollectionFoodModel(view);
                collectionFoodModel.getResultList(user_id,food_id);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(shopDateList!=null)
            return shopDateList.size();
        else
            return 0;
//        return shopDateList.size();//返回列表显示的数据个数
    }
}
